package com.hk.utils;

import com.hk.model.Document;

import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/22 15:36
 * State：Io创建读写工具类
 */
public class Io {

    static String path;
//    固定项目路径
    public static File publicPath() {
//        System.out.println("项目路径："+path);
        StringBuffer stringBuffer = new StringBuffer(path);
        //    无论读还是写都是这个路径
        String path1 = String.valueOf(stringBuffer.append("\\web\\text"));
//        System.out.println("存储总路径："+path1);
        File childPath = new File(path1);
        return childPath;
    }

    /**
     * 检索或创建
     * 地址
     * 文件夹
     * 文件
     *
     * @param document
     */
    public static void createDirectoryFile(Document document) {
//        获取当前项目名称
        path  = document.getProjectName();
//        判断该路径是否存在，不存在则创建该路径
        if (!publicPath().exists()) {
            publicPath().mkdirs();
            System.out.println(publicPath()+"路径"+"创建成功");
        }
        File directory1 = new File(publicPath(), document.getDirectory());
//        判断该文件夹是否存在
        if (!directory1.isDirectory()) {
            directory1.mkdirs();
            System.out.println(publicPath()+"路径下的"+document.getDirectory()+"文件夹"+"创建成功");
        }
        File fielName1 = new File(directory1, document.getFileName());
//        判断该文件是否存在
        if (!fielName1.isFile()) {
            try {
                fielName1.createNewFile();
                System.out.println(publicPath()+"路径下的"+document.getDirectory()+"文件夹下的"+document.getFileName()+"创建成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 写入数据
     * 写入到哪个文件夹下的哪个文件里
     *
     * @param list
     * @param document
     */
    public static void writeData(List<String> list,Document document) {
//        检索或创建该文件夹或文件
        createDirectoryFile(document);
        BufferedOutputStream bufferedOutputStream = null;
        OutputStream outputStream = null;
        try {
            File directory1 = new File(publicPath(), document.getDirectory());
            File file = new File(directory1,document.getFileName());
            outputStream = new FileOutputStream(file);
            bufferedOutputStream = new BufferedOutputStream(outputStream);
//            开始遍历写入数据
            for (String s : list) {
                bufferedOutputStream.write(s.getBytes());
                bufferedOutputStream.write(new Document().getSplit().getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.flush();
                outputStream.close();
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取数据
     * @param document
     */
    public static List<String> readData(Document document) {
        List<String> list = new ArrayList<>();
//       处理流
        BufferedReader bufferedReader = null;
        Reader reader = null;
//       数据中存在中文，使用字符流
        try {
//          获取读取文件的路径file
            File directory1 = new File(publicPath(), document.getDirectory());
            File file = new File(directory1,document.getFileName());
            reader = new FileReader(file);
            bufferedReader = new BufferedReader(reader);
            String findAll;
            while (((findAll = bufferedReader.readLine()) != null)) {
                list.add(findAll);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
